﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using DTO;
namespace GUI
{
    public partial class frmDanhSachDonHang : Form
    {
        public frmDanhSachDonHang()
        {
            InitializeComponent();
        }
        private int _maHD;

        public int MaHD
        {
            get { return _maHD; }
        }
        private void frmDanhSachDonHang_Load(object sender, EventArgs e)
        {
            HoaDonBUS hdBUS = new HoaDonBUS();
            KhachHangBUS khBUS = new KhachHangBUS();
            int tatca = 2;
            List<HoaDonDTO> listHD = hdBUS.LayDanhSachHoaDonL(tatca);
            for (int i = 0; i < listHD.Count; i++)
            {
                if (hdBUS.LayThoiGianGiaoL(listHD[i].MaHoaDon) <= 0)
                {
                    int dagiao = 1;
                    string mess = hdBUS.CapNhatTinhTrang(listHD[i].MaHoaDon, dagiao);
                }
            }
            TaoDuLieuCombobox();

           
        }
        private void LayDanhSachHoaDon(int maHD)
        {
            int stt = dgvDanhSachDonHang.Rows.Count;
            HoaDonBUS hdBUS = new HoaDonBUS();
            KhachHangBUS khBUS = new KhachHangBUS();
            List<HoaDonDTO> listHD = hdBUS.LayDanhSachHoaDonL(maHD);
            for (int i = 0; i < listHD.Count; i++)
            {
                stt += 1;
                KhachHangDTO khDTO = new KhachHangDTO();
                khDTO = khBUS.LayTenThongTinKhachHangTheoMaL(listHD[i].MaKhachHang);
                string tinhtrang = "";
                tinhtrang = TinhTrang(listHD[i].TinhTrang);
                dgvDanhSachDonHang.Rows.Add(stt, listHD[i].MaHoaDon, khDTO.TenKhachHang, khDTO.SoDienThoai, listHD[i].GiaoDen, listHD[i].NgayLap, tinhtrang);
            }
        }
        private string TinhTrang(int tt)
        {
            string tinhtrang = "";
            switch (tt)
            {
                case 0: tinhtrang = "Chưa giao";
                    break;
                case 1: tinhtrang = "Đã giao";
                    break;
                default: tinhtrang = "Đã hủy";
                    break;
            }
            return tinhtrang;
        }
        private void TaoDuLieuCombobox()
        {
            cbbLoaiHoaDon.Items.Add("Tất cả ");
            cbbLoaiHoaDon.Items.Add("Hóa đơn bán hàng");
            cbbLoaiHoaDon.Items.Add("Hóa đơn đặt hàng");
            cbbLoaiHoaDon.SelectedIndex = 0;
        }

        private void cbbLoaiHoaDon_SelectedIndexChanged(object sender, EventArgs e)
        {
            int maHD = int.Parse(cbbLoaiHoaDon.SelectedIndex.ToString());
            dgvDanhSachDonHang.Rows.Clear();
            LayDanhSachHoaDon(maHD);
        }

        private void cbbLoaiHoaDon_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.KeyChar = (char)0;
        }

        private void dgvDanhSachDonHang_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

            int dong = dgvDanhSachDonHang.SelectedCells.Count;
            if (dong == 0)
                return;
            dong = dgvDanhSachDonHang.SelectedCells[0].RowIndex;
            _maHD = int.Parse(dgvDanhSachDonHang.Rows[dong].Cells[1].Value.ToString());
            frmThongTinDonHang fr = new frmThongTinDonHang();
            fr.MaHD = MaHD;
            fr.ShowDialog();
        }

        private void btnexit_Click(object sender, EventArgs e)
        {
            //DialogResult dlr = MessageBox.Show("Bạn muốn thoát khỏi chương trình?", "Warnning", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            //if (dlr == DialogResult.Yes)
            //    this.Close();
            Close();
        }
    }
}
